package com.bai.order.controller;


import com.bai.order.entity.Order;
import com.bai.order.service.OrderService;
import com.bai.utils.JwtUtils;
import com.bai.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author bai
 * @since 2023-08-19
 */
@RestController
@RequestMapping("/edu/order")
@CrossOrigin
public class OrderController {
    @Autowired
    private OrderService orderService;


    //TODO:生成订单
    @PostMapping("createOrder/{courseId}")
    public R saveOrder(@PathVariable String courseId, HttpServletRequest request) {
        String id = JwtUtils.getMemberIdByJwtToken(request);
        //根据课程id和用户id创建订单
        String orderNo = orderService.createOrders(courseId, id);
        return R.ok().data("orderNo",orderNo);
    }

    //TODO:根据订单id查询订单信息
    @GetMapping("getOrderInfo/{OrderId}")
    public R getOrderInfo(@PathVariable String OrderId){
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no",OrderId);
        Order order = orderService.getOne(queryWrapper);
        return R.ok().data("order",order);
    }

    //TODO:根据课程id和用户id查询订单表中的订单状态
    @GetMapping("isBuyCourse/{courseId}/{memberId}")
    public boolean isBuyCourse(@PathVariable String courseId,
                               @PathVariable String memberId){
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);
        queryWrapper.eq("member_id",memberId);
        queryWrapper.eq("status",1);
        int count = orderService.count(queryWrapper);
        if (count>0){//已经支付
            return true;
        }else {//未支付
            return false;
        }
    }
}

